package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description: 对象锁 第二种形式 synchronized 修饰普通的方法 锁默认是this 不是静态方法
 * @date: 2019/1/26 12:31
 * @version: V1.0
 */
public class SynchronizedObjectMethod3 implements Runnable {

    static SynchronizedObjectMethod3 instance = new SynchronizedObjectMethod3();

    public void run() {
        method();
    }


    public synchronized void method() {
        System.out.println("我是对象锁的lock0 :  代码块形式：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "：运行完成");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(instance);
        Thread thread1 = new Thread(instance);
        thread.start();
        thread1.start();
        while (thread.isAlive() || thread1.isAlive()) {
        }
        System.out.println("Finished...");
    }
}
